﻿using System;

namespace CalculatingInfiniteSum
{
    class CalculatingInfiniteSum
    {
        public delegate void FunctionDelegate();

        static void Main(string[] args)
        {
            FunctionDelegate functions = new FunctionDelegate(FirstFunction);
            functions += SecondFunction;
            functions += ThirdFunction;
            functions();
        }

        private static void FirstFunction()
        {
            double result;
            double nextNumber = 0;
            int iterator = 0;
            do
            {
                result = nextNumber;
                nextNumber = result + 1 / (Math.Pow(2, iterator));
                iterator++;
            }
            while (nextNumber - result >= 0.01);
            //while ((1/Math.Pow(2, iterator))>=0.01)
            //{
            //    result = result + 1 / (Math.Pow(2, iterator));
            //    iterator++;
            //}
            Console.WriteLine(result);
        }

        private static void SecondFunction()
        {
            double result;
            double nextNumber = 0;
            int iterator = 0;
            do
            {
                result = nextNumber;
                nextNumber = result + 1 / Factoriel(iterator);
                iterator++;
            }
            while (nextNumber - result >= 0.01);
            Console.WriteLine(result);
        }

        private static void ThirdFunction()
        {
            double result;
            double nextNumber = 0;
            int iterator = 0;
            do
            {
                result = nextNumber;
                nextNumber = result + 1 / (Math.Pow(-2, iterator));
                iterator++;
            }
            while (nextNumber - result >= 0.01 || result - nextNumber >= 0.01);
            Console.WriteLine(result);
        }

        private static double Factoriel(int number)
        {
            int result = 1;
            for (int i = 2; i < number + 1; i++)
            {
                result *= i;
            }
            return result;
        }
    }
}